<?php
namespace App\Controller;



use App\Model\ItemsModel;
use App\Model\TopicModel;
use Common\Util\CacheUtils;
use Common\Util\ConfigUtils;

class IndexController extends BaseController
{

    //主题列表
    public function topicList(){
        $this->validateParam(array('city_code'));
        $city_code= $this->getPostParam('city_code');
        $cacheName = $city_code."__TOPIC_LIST";
        $list= CacheUtils::get($cacheName);
        if(!$list){
            $list   =  TopicModel::singleton()->getListByWhere($city_code);
            CacheUtils::set($cacheName,$list,86400);

        }
        $this->ok($list);
    }

    //单品列表
    public function itemList(){
        $this->validateParam(array('city_code','topic_code'));
        $city_code= $this->getPostParam('city_code');
        $topic_code=$this->getPostParam('topic_code');
        $page_no =$this->getPostParam('page_no',1);
        $page_size =$this->getPostParam('page_size',20);
        $list = ItemsModel::singleton()->getList($topic_code,$city_code,$page_no,$page_size);
        $topic_info = TopicModel::singleton()->where(['topic_code'=>$topic_code])->find();

        $child_image = ConfigUtils::getConfig('SERVER_URL').$topic_info['child_image'];

        $result = [
            'result' => 200,
            'msg' => 'ok',
            'server_time' => (string)time(),
            'child_image'=>$child_image,
            'data' => $list,
        ];
        header('Content-Type:application/json; charset=UTF-8');
        exit(json_encode($result));

    }

    public function clearCache(){
        $this->validateParam(array('city_code'));
        $city_code= $this->getPostParam('city_code');
        $cacheName = $city_code."__TOPIC_LIST";
        CacheUtils::set($cacheName,null);
    }
}